package io.gitee.chearnee.fw.common.component;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.time.LocalDateTime;

/**
 * @author cn
 * @version 1.0
 * @description
 * @date 2021/6/23 11:32
 */
public class CustomLocalDateTimeDeserializer extends StdDeserializer<LocalDateTime> {

  public CustomLocalDateTimeDeserializer() {
    this(null);
  }

  protected CustomLocalDateTimeDeserializer(Class<LocalDateTime> vc) {
    super(vc);
  }

  @Override
  public LocalDateTime deserialize(JsonParser p, DeserializationContext ctxt)
      throws IOException {
    String longValue = p.getValueAsString();
    if (!StringUtils.hasText(longValue)) {
      return null;
    }
    return LocalDateTimeUtil.of(Long.parseLong(longValue));
  }
}